package com.zhongge.jdbc01;

import com.zhongge.util.DbUtil;

import java.sql.*;

/**
 * @ClassName JdbcDbUtilTest
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
public class JdbcDbUtilTest {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet re = null;
        try {
            //获取连接(一旦加载DbUtil这个类就会自动注册驱动)
            conn = DbUtil.getConnection();

            //获取操作数据库的对象
            stmt = conn.createStatement();

            //执行sql语句
            //当获取到Statement对象后，调用这个接口中的相关方法即可执行SQL语句。
            /**
             * ResultSet ： 结果集
             */
            re = stmt.executeQuery("select * from book");

            //处理结果
            while (re.next()) {
                String name = re.getString("name");
                int price = re.getInt("price");
                String author = re.getString("author");
                String type = re.getString("type");
                Boolean isBorrow = re.getBoolean("isBorrow");
                System.out.println(name + "\t\t" + price + "\t\t" + author + "\t\t" + type + "\t\t" + isBorrow);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtil.close(conn, stmt, re);
        }
    }
}
